// okienko odbijania obecnosci

unit Unit10;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, IBQuery, IBDatabase, ZDataset;

type

  { TForm10 }

  TForm10 = class(TForm)
    Edit1: TEdit;
    ibq: TIBQuery;
    ibt: TIBTransaction;
    Label1: TLabel;
    Memo1: TMemo;
    Timer1: TTimer;
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form10: TForm10;

implementation

{$R *.lfm}

{ TForm10 }

procedure TForm10.Timer1Timer(Sender: TObject);
begin
  form10.ActiveControl:=form10.Edit1;
  if label1.Font.Color = clBlue then label1.Font.Color:=clRed else label1.Font.Color:=clBlue;
end;

procedure TForm10.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  prac_nazwa : string;
  prac_idx   : integer;
  rec        : integer;
  rec_cnt    : integer;
  dt_st,czas,teraz : TDateTime;
begin
  if Key = 13 then
  begin
    if ibq.Transaction.Active then ibq.Transaction.Rollback;

    // wyszukanie pracownika, który sie wlasie odbil kodem EAN
    ibq.SQL.Clear;
    ibq.SQL.Add('select pr.nazwadl, pr.pracownik_idx from pracownik pr where pr.ean = :a');
    ibq.ParamByName('a').AsString:=edit1.Text;
    try
      ibq.Transaction.StartTransaction;
      ibq.Open;
      rec_cnt:=ibq.RecordCount;
      if rec_cnt <> 0 then
      begin
        prac_nazwa:=ibq.FieldByName('nazwadl').AsString;
        prac_idx  :=ibq.FieldByName('pracownik_idx').AsInteger;
      end;
      ibq.Transaction.Commit;
    except
      ibq.Transaction.Rollback;
    end;

    if rec_cnt = 0 then memo1.Lines.Add(DateTimeToStr(now())+' : nieznany kod')
    else
      begin
        ibq.SQL.Clear;
        ibq.SQL.Add('select * from obecnosci ob where ob.ean = :a and ob.data_stop is null');
        ibq.ParamByName('a').AsString:=edit1.Text;
        try
          ibq.Transaction.StartTransaction;
          ibq.Open;

          if ibq.RecordCount <> 0 then
          begin
            dt_st:=ibq.FieldByName('data_start').AsDateTime;
            rec:=ibq.fieldbyname('obecnosci_idx').AsInteger;
          end
            else rec:=-1;
          ibq.Transaction.Commit;
        except
          ibq.Transaction.Rollback;
        end;

        // znaleziono przynajmniej jeden rekord z pustym wyjsciem dla tego ean
        if rec <> -1 then
        begin
          teraz:=Now();
          czas:=teraz - dt_st;
          memo1.Lines.Add(DateTimeToStr(now())+' : wyjscie -> '+prac_nazwa+' ('+TimeToStr(czas)+')');

          ibq.SQL.Clear;
          ibq.SQL.Add('update obecnosci set data_stop = :a,czas_pracy = :c where obecnosci_idx = :b');
          ibq.ParamByName('a').AsDateTime:=teraz;
          ibq.ParamByName('b').AsInteger:=rec;
          ibq.ParamByName('c').AsTime:=czas;
          try
            ibq.Transaction.StartTransaction;
            ibq.ExecSQL;
            ibq.Transaction.Commit;
          except
            ibq.Transaction.Rollback;
          end;
        end
        else // nie znaleziono wejscia wiec je rejestrujemy
        begin
          memo1.Lines.Add(DateTimeToStr(now())+' : wejscie <- '+prac_nazwa);
          ibq.SQL.Clear;
          ibq.SQL.Add('insert into obecnosci (data_start, ean, pracownik_idx) values (:a,:b,:c)');
          ibq.ParamByName('a').AsDateTime:=Now();
          ibq.ParamByName('b').AsString:=edit1.Text;
          ibq.ParamByName('c').AsInteger:=prac_idx;
          try
            ibq.Transaction.StartTransaction;
            ibq.ExecSQL;
            ibq.Transaction.Commit;
          except
            ibq.Transaction.Rollback;
          end;
        end
      end;
    edit1.Clear;
  end;
end;

procedure TForm10.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  timer1.Enabled:=false;
end;

procedure TForm10.FormShow(Sender: TObject);
begin
  timer1.Enabled:=true;
end;

end.

